package com.chaofan.authsso.core.common;

import com.chaofan.authsso.core.service.CacheInstance;
import com.chaofan.authsso.core.util.CacheKeyUtil;

import java.io.Serializable;
import java.util.UUID;

/**
 * Author: Chaofan
 * Date: 2020/6/5 16:32
 * email: chaofan2685@qq.com
 */
public class AuthToken implements Serializable {

    private static final long serialVersionUID = 176334269571212457L;
    //验证码
    private String authCode;
    //token
    private String token;

    public AuthToken(String token) {
        this.token = token;
    }

    public AuthToken(String authCode, String token) {
        this.authCode = authCode;
        this.token = token;
    }

    public AuthToken(CacheInstance cacheInstance) {
        do {
            this.authCode = UUID.randomUUID().toString().substring(0,8);
        }while (cacheInstance.containsKey(CacheKeyUtil.authCodeKey(this.authCode)));
        do {
            this.token = UUID.randomUUID().toString();
        }while (cacheInstance.containsKey(CacheKeyUtil.tokenKey(this.token)) || cacheInstance.containsKey(CacheKeyUtil.applyTokenKey(this.token)));
    }

    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

}
